package com.bosssoft.controller;

import com.bosssoft.permission.centre.entity.Department;
import com.bosssoft.permission.centre.entity.Position;
import com.bosssoft.permission.centre.entity.Role;
import com.bosssoft.permission.centre.entity.User;
import com.bosssoft.permission.centre.pojo.dto.pcp.UserDTO;
import com.bosssoft.permission.centre.pojo.query.UserQuery;
import com.bosssoft.permission.centre.pojo.vo.ResourceVO;
import com.bosssoft.permission.centre.pojo.vo.UserVO;
import com.bosssoft.permission.centre.service.LoginService;
import com.bosssoft.permission.centre.service.UserService;
import com.sd365.common.core.common.controller.AbstractController;
import com.sd365.common.core.common.exception.BusinessException;
import com.sd365.common.core.common.exception.code.BusinessErrorCode;
import com.sd365.common.log.api.annotation.ApiLog;
import com.sd365.common.util.BeanUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import com.bosssoft.permission.centre.api.UserApi;
import javax.validation.Valid;
import java.sql.Timestamp;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: wang
 * @Time: 2022/3/23
 */
@Slf4j
@RestController
public class UserController extends AbstractController implements UserApi {
    @Autowired
    private UserService userService;
    @Autowired
    LoginService loginService;


    @Override
    public List<ResourceVO> getUserResourceVO(Long id) {
        List<Role> roles = loginService.getRolesByUserId(id);
        List<String> rolesName = new LinkedList<>();
        for(Role role : roles) {
            rolesName.add(role.getName());
        }
        List<ResourceVO> resourceVO = loginService.getResourceVO(roles);
        return resourceVO;
    }

    @ApiLog
    @Override
    public Boolean add(@Valid UserDTO userDTO) {
        log.info(userDTO.getName());
        log.info(userDTO.getCode());
        log.info(userDTO.getPassword());
        if (null == userDTO) {
            throw new BusinessException(BusinessErrorCode.SYSTEM_CONTROLLER_ARGUMENT_NOT_VALID, new Exception("添加对象不允许为空"));
        }
        log.info("打印增加的userDTO信息");
        log.info(userDTO.toStr());
//        java.util.Date date=new java.util.Date();
//        Timestamp timestamp = new Timestamp(date.getTime());
//        userDTO.setCreatedTime(timestamp);
//        userDTO.setUpdatedTime(timestamp);
        return userService.add(userDTO);
    }

    @ApiLog
    @Override
    public Boolean remove(Long id, Long version) {
        return userService.remove(id, version);
    }

    @ApiLog
    @Override
    public Boolean modify(@Valid UserDTO userDTO) {
        if (null == userDTO) {
            throw new BusinessException(BusinessErrorCode.SYSTEM_CONTROLLER_ARGUMENT_NOT_VALID, new Exception("修改对象不允许为空"));
        }
        return userService.modify(userDTO);
    }

    @Override
    public Boolean modifyWithNewRole(@Valid UserDTO[] userDTOS) {
        return userService.modifyWithNewRole(userDTOS);
    }

    @ApiLog
    @Override
    public List<User> commonQuery(UserQuery userQuery) {
        if (null == userQuery) {
            throw new BusinessException(BusinessErrorCode.SYSTEM_CONTROLLER_ARGUMENT_NOT_VALID, new Exception("查询对象不允许为空"));
        }

        List<User> userDTOS = userService.commonQuery(userQuery);

        return userDTOS;
    }


    @Override
    public UserVO queryUserById(Long id) {
        User user = userService.queryById(id);
        if(user==null){
            return new UserVO();
        }
        if (user!=null){
            UserVO userVO = BeanUtil.copy(user,UserVO.class);
            return userVO;
        }else {
            return null;
        }
    }

    @Override
    public List<Role> queryAllRole() {
        return userService.queryAllRole();
    }

    @Override
    public List<Department> queryAllDepartment() {
        return userService.queryAllDepartment();
    }

    @Override
    public List<Position> queryAllPosition() {
        return userService.queryAllPosition();
    }

    @Override
    public Boolean batchUpdate(@Valid UserDTO[] userDTOS) {
        return userService.batchUpdate(userDTOS);
    }

    @Override
    public Boolean batchDelete(@Valid UserDTO[] userDTOS) {
        return userService.batchDelete(userDTOS);
    }

    @Override
    public Boolean firstStartMd5() {
       return true;
    }
}
